﻿package UIComponents
{
	import MyComponent.MyButton;
	
	import fl.events.ComponentEvent;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	
	public class MakingFrameListPanel extends Sprite
	{
		public var mkItemName:TextField;
		public var mkItemPic:IconContainer;
		public var mkBtn:MyButton;
		public var mflmb:DisplayObject;
		private var imission:IMission;
		
		public function MakingFrameListPanel()
		{
			init();
		}
		
		private function init():void
		{
			this.mkItemName=this.getChildByName("makingItemName") as TextField;
			var sp:DisplayObjectContainer=this.getChildByName("makingResult") as DisplayObjectContainer;
			this.mkItemPic=sp.getChildByName("makingItemSp") as IconContainer;
			this.mkBtn=sp.getChildByName("button_use") as MyButton;
			this.mflmb=sp.getChildByName("noItemmkSp");
			inintLister();
		}
		private function inintLister():void
		{
			this.mkBtn.addEventListener(ComponentEvent.BUTTON_DOWN,makeFun);
		}
		
		private function getFrameCollectByIndex(index:int):MakingFrameItemPanel
		{
			if(index<0 || index>4)throw new Error("索引出错");
			return (this.getChildByName("collectItem_"+(index+1))) as MakingFrameItemPanel;
		}
		
		public function setData(imission:IMission):void
		{
			this.reset();
			var btnFlag:Boolean=true;
			this.imission=imission;
			var conditionCountsAry:Array=imission.conditionCountsAry;
			var rewardCountsAry:Array=imission.rewardCountsAry;
			var iconValue:DisplayObject=rewardCountsAry[0];
			this.mkItemPic.addPic(iconValue,iconValue["displayViewIconName"]);
			this.mkItemName.text=this.imission.missionName;
			this.mflmb.visible=false;
			for(var i:int=0;i<5;i++)
			{
				var frameCollectSuit:MakingFrameItemPanel=this.getFrameCollectByIndex(i);
				frameCollectSuit.reset();
				frameCollectSuit.mfmb.visible=true;
				var obj:Object=conditionCountsAry[i];
				if(null!=obj)
				{
					var tmpFlag:Boolean=frameCollectSuit.setFrameData(obj);
					if(!tmpFlag)
					{
						btnFlag=tmpFlag;
					}
//					frameCollectSuit.visible=true;
				}//else{
//					frameCollectSuit.visible=true;
//					btnFlag=false;
//				}
			}
			
			if(!btnFlag)
			{
				this.mkBtn.enabled=false;
			}else{
				this.mkBtn.enabled=true;
			}
		}
		
		public function reset():void
		{
			this.mkItemName.text="";
			this.mkItemPic.addPic(null);
			this.mflmb.visible=true;
			this.mkBtn.enabled=false;
			for(var i:int=0;i<5;i++)
			{
				var frameCollectSuit:MakingFrameItemPanel=this.getFrameCollectByIndex(i);
				frameCollectSuit.reset();
			}
		}
		
		public function makeFun(e:ComponentEvent):void
		{
			this.imission.finishHandler();
			this.dispatchEvent(new Event("button_use",true));
		}
	}
}